package org.esiag.isidis.container;

import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;

import org.esiag.isidis.proxy.BeanInvocationHandler;
import org.omg.PortableInterceptor.Interceptor;

public class Container {
	
	private Map<Class<?>, Class<?>> registry;

    public Container() {
        registry = new HashMap<Class<?>, Class<?>>();
    }

	public <T> Container register(Class<? extends T> impl, Class<T> iface) {
		registry.put(iface, impl);
		return this;
	}

    public <T> T createBean(Class<T> iface) {
        try {
            Class<? extends T> impl = (Class<? extends T>) registry.get(iface);
            BeanInvocationHandler handler = new BeanInvocationHandler(
                    impl.newInstance());
            return (T) Proxy.newProxyInstance(
                    Thread.currentThread().getContextClassLoader(),
                    new Class[] {iface},
                    handler);
        } catch (Exception e) {
            throw new ContainerException(e.getMessage());
        }
    }

}
